package com.huawei.petSystemdemo.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.huawei.petSystemdemo.R;
import com.huawei.petSystemdemo.bean.Pets;
import com.huawei.petSystemdemo.utils.ImgRepository;

import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.function.Consumer;

public class OrderPetAdapter extends RecyclerView.Adapter<OrderPetAdapter.OrderPetViewHolder> {

    private List<Pets> petsList;
    private Context context;

    public OrderPetAdapter(List<Pets> petsList, Context context) {
        this.petsList = petsList;
        this.context = context;
    }

    public class OrderPetViewHolder extends RecyclerView.ViewHolder {

        TextView order_pet_name;
        ImageView order_pet_img;
        TextView order_pet_price;

        public OrderPetViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);

            order_pet_name = itemView.findViewById(R.id.order_pet_name);
            order_pet_img = itemView.findViewById(R.id.order_pet_img);
            order_pet_price = itemView.findViewById(R.id.order_pet_price);
        }
    }

    @NonNull
    @NotNull
    @Override
    public OrderPetAdapter.OrderPetViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.comment_order_pet, parent, false);
        return new OrderPetAdapter.OrderPetViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull OrderPetAdapter.OrderPetViewHolder holder, int position) {
        Pets pet = petsList.get(position);
        holder.order_pet_name.setText(pet.getName());
        holder.order_pet_price.setText(pet.getPrice() + "");
        ImgRepository.getImage("/pet/getImg?img=" + pet.getImg(), new Consumer<Bitmap>() {
            @Override
            public void accept(Bitmap bitmap) {
                holder.order_pet_img.setImageBitmap(bitmap);
            }
        });
    }

    @Override
    public int getItemCount() {
        return petsList.size();
    }
}
